using System;
using System.Collections.Generic;
using System.Threading;
using GZ.DB.Entity.wcs_hy;

namespace GZ.Projects.WCS_YB
{
	/// <summary>
	/// 堆垛机发送任务到Redis
	/// </summary>
	public class StackerSendToRedis
	{

		public bool SendTask_SingleStation(string type, StackerTaskEntity task, out string err)
		{
			err = "";
			try
			{
				string num = task.DeviceNo.Substring(task.DeviceNo.Length - 2);
				string groupName = $"TC{num}.sendTcTask_{num}";
				string setQueue = $"TC{num}Queue";
				string srmNo = $"TC{num}.wcs_srmNo";
				string commandType = $"TC{num}.wcs_commandType";
				string srcZs1 = $"TC{num}.wcs_source_Z_S1";
				string srcXs1 = $"TC{num}.wcs_source_X_S1";
				string srcYs1 = $"TC{num}.wcs_source_Y_S1";
				//string srcLS1 = $"TC{num}.wcs_source_L_S1";
				string destZs1 = $"TC{num}.wcs_dest_Z_S1";
				string destXs1 = $"TC{num}.wcs_dest_X_S1";
				string destYs1 = $"TC{num}.wcs_dest_Y_S1";
				//string destLS1 = $"TC{num}.wcs_dest_L_S1";
				string taskS1 = $"TC{num}.wcs_task_S1";
				string contType1 = $"TC{num}.wcs_goodType1";
				string confirmSignal = $"TC{num}.wcs_confirmSignal1"; //1-开始执行任务 3-工位1任务正常完成确认 8-上位写8为任务删除 10-收到上位强制完成信号


				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = srmNo, paramValue = int.Parse(num).ToString() },
						new GZ.Modular.Redis.ParamData
							{ paramName = commandType, paramValue = type }, //单工位的堆垛机，命令类型工位1搬运即可
						new GZ.Modular.Redis.ParamData { paramName = srcZs1, paramValue = task.FromX.ToString() }, //排
						new GZ.Modular.Redis.ParamData { paramName = srcXs1, paramValue = task.FromY.ToString() }, //列
						new GZ.Modular.Redis.ParamData { paramName = srcYs1, paramValue = task.FromZ.ToString() }, //层
						//new GZ.Modular.Redis.ParamData{paramName = srcLS1,paramValue = fromP}, //伸位
						new GZ.Modular.Redis.ParamData { paramName = destZs1, paramValue = task.ToX.ToString() },
						new GZ.Modular.Redis.ParamData { paramName = destXs1, paramValue = task.ToY.ToString() },
						new GZ.Modular.Redis.ParamData { paramName = destYs1, paramValue = task.ToZ.ToString() },
						//new GZ.Modular.Redis.ParamData{paramName = destLS1,paramValue = toP},
						new GZ.Modular.Redis.ParamData { paramName = taskS1, paramValue = task.DeviceTaskNo },
						new GZ.Modular.Redis.ParamData { paramName = contType1, paramValue = task.TrayType }
					}
				};

				if (Conn.YBRedis.SetQueue(group, setQueue, "", "")) //要确保位置信息、命令类型等已写入
				{
					Thread.Sleep(100);
					Conn.YBRedis.SetValue(confirmSignal, "1", setQueue);
					return true;
				}
				return false;
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}
		}
		public bool SendLast_SingleStation(string num, out string err)
		{
			err = "";
			try
			{
				string suffixName = $"TC{num}";
				string groupName = $"TC{num}.feedbackClear_{num}";
				string srmNo = $"TC{num}.wcs_srmNo";
				string commandType = $"TC{num}.wcs_commandType";
				string confirmSignal = $"TC{num}.wcs_confirmSignal1"; //1-开始执行任务 3-工位1任务正常完成确认 8-上位写8为任务删除 10-收到上位强制完成信号
				string setQueue = $"TC{num}Queue";
				string setLast = $"{suffixName}.plc_taskStatus1_LAST";

				bool result = false;

				//LAST的值置位
				//tagname=redis名称  value- 值 key -queue
				//result = Conn.YBRedis.SetValue(setLast, "0", setQueue);
				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = srmNo, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = commandType, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = confirmSignal, paramValue = "0" }
					}
				};
				result = Conn.YBRedis.SetQueue(group, setQueue, setLast, "0");
				return result;
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}
		}
		
		public bool ClearTask_SingleStation(string num, string type, string wcsConfirm, out string err)
		{
			err = "";
			try
			{
				string groupName = $"TC{num}.sendTcTask_{num}";
				string setQueue = $"TC{num}Queue";
				string srmNo = $"TC{num}.wcs_srmNo";
				string commandType = $"TC{num}.wcs_commandType";
				string srcZs1 = $"TC{num}.wcs_source_Z_S1";
				string srcXs1 = $"TC{num}.wcs_source_X_S1";
				string srcYs1 = $"TC{num}.wcs_source_Y_S1";
				string destZs1 = $"TC{num}.wcs_dest_Z_S1";
				string destXs1 = $"TC{num}.wcs_dest_X_S1";
				string destYs1 = $"TC{num}.wcs_dest_Y_S1";
				string taskS1 = $"TC{num}.wcs_task_S1";
				string contType1 = $"TC{num}.wcs_goodType1";
				string confirmSignal = $"TC{num}.wcs_confirmSignal1"; //1-开始执行任务 3-工位1任务正常完成确认 8-上位写8为任务删除 10-收到上位强制完成信号
				string setLast = $"TC{num}.plc_taskStatus1_LAST";
				//这里应该是去记录commandType的LAST
				string plcTaskStatus1 = 
					wcsConfirm == "8" ? "6" :
					wcsConfirm == "10" ? "5" :
					wcsConfirm == "3" ? "3" :
					wcsConfirm == "1" ? "1" : "0";
				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = srmNo, paramValue = int.Parse(num).ToString() },
						new GZ.Modular.Redis.ParamData
							{ paramName = commandType, paramValue = type }, //单工位的堆垛机，命令类型工位1搬运即可
						new GZ.Modular.Redis.ParamData { paramName = srcZs1, paramValue = "0" }, //排
						new GZ.Modular.Redis.ParamData { paramName = srcXs1, paramValue = "0" }, //列
						new GZ.Modular.Redis.ParamData { paramName = srcYs1, paramValue = "0" }, //层
						new GZ.Modular.Redis.ParamData { paramName = destZs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destXs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destYs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskS1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = contType1, paramValue = "0" }
					}
				};

				if (Conn.YBRedis.SetQueue(group, setQueue, setLast, plcTaskStatus1))
				{
					return Conn.YBRedis.SetValue(confirmSignal, wcsConfirm, setQueue);
					// return true;
				}

				return false;
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}
		}
		
		public bool ManualTask_SingleStation(string type, StackerTaskEntity task, out string err)
		{
			err = "";
			try
			{	
				string num = task.DeviceNo.Substring(task.DeviceNo.Length - 2);
				string groupName = $"TC{num}.sendTcTask_{num}";
				string setQueue = $"TC{num}Queue";
				string srmNo = $"TC{num}.wcs_srmNo";
				
				string commandType = $"TC{num}.wcs_commandType";
				string srcZs1 = $"TC{num}.wcs_source_Z_S1";
				string srcXs1 = $"TC{num}.wcs_source_X_S1";
				string srcYs1 = $"TC{num}.wcs_source_Y_S1";
				string destZs1 = $"TC{num}.wcs_dest_Z_S1";
				string destXs1 = $"TC{num}.wcs_dest_X_S1";
				string destYs1 = $"TC{num}.wcs_dest_Y_S1";
				string taskS1 = $"TC{num}.wcs_task_S1";
				string contType1 = $"TC{num}.wcs_goodType1";
				string confirmSignal = $"TC{num}.wcs_confirmSignal1"; //1-开始执行任务 3-工位1任务正常完成确认 8-上位写8为任务删除 10-收到上位强制完成信号
				string setLast = $"TC{num}.plc_taskStatus1_LAST";
				//这里应该是去记录commandType的LAST
				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = srmNo, paramValue = int.Parse(num).ToString() },
						new GZ.Modular.Redis.ParamData
							{ paramName = commandType, paramValue = type }, //单工位的堆垛机，命令类型工位1搬运即可
						new GZ.Modular.Redis.ParamData { paramName = srcZs1, paramValue = "0" }, //排
						new GZ.Modular.Redis.ParamData { paramName = srcXs1, paramValue = "0" }, //列
						new GZ.Modular.Redis.ParamData { paramName = srcYs1, paramValue = "0" }, //层
						new GZ.Modular.Redis.ParamData { paramName = destZs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destXs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destYs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskS1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = contType1, paramValue = "0" }
					}
				};

				if (Conn.YBRedis.SetQueue(group, setQueue, "", ""))
				{
					return Conn.YBRedis.SetValue(confirmSignal, "1", setQueue);
					// return true;
				}

				return false;
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}
		}
		
		public bool SendTask_DoubleStation(StackerTaskEntity task1,StackerTaskEntity task2, out string err)
		{
			err = "";
			//加一个判断，如果task1或者task2为空，则task1或者task2的相关值为0，
			try
			{
				string groupName = $"TC01.sendTcTask_01";
				string setQueue = $"TC01Queue";
				string srmNo = $"TC01.wcs_srmNo";
				string commandType = $"TC01.wcs_commandType";
				string srcZs1 = $"TC01.wcs_source_Z_S1";
				string srcXs1 = $"TC01.wcs_source_X_S1";
				string srcYs1 = $"TC01.wcs_source_Y_S1";
				//string srcLS1 = $"TC{num}.wcs_source_L_S1";
				string destZs1 = $"TC01.wcs_dest_Z_S1";
				string destXs1 = $"TC01.wcs_dest_X_S1";
				string destYs1 = $"TC01.wcs_dest_Y_S1";
				//string destLS1 = $"TC{num}.wcs_dest_L_S1";
				string taskS1 = $"TC01.wcs_task_S1";
				string contType1 = $"TC01.wcs_goodType1";
				string confirmSignal1 = $"TC01.wcs_confirmSignal1"; //1-开始执行任务 3-工位1任务正常完成确认 8-上位写8为任务删除 10-收到上位强制完成信号

				string srcZs2 = $"TC01.wcs_source_Z_S2";
				string srcXs2 = $"TC01.wcs_source_X_S2";
				string srcYs2 = $"TC01.wcs_source_Y_S2";
				string destZs2 = $"TC01.wcs_dest_Z_S2";
				string destXs2 = $"TC01.wcs_dest_X_S2";
				string destYs2 = $"TC01.wcs_dest_Y_S2";
				string taskS2 = $"TC01.wcs_task_S2";
				string contType2 = $"TC01.wcs_goodType2";
				string confirmSignal2 = $"TC01.wcs_confirmSignal2";

				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = srmNo, paramValue = "1" },
						new GZ.Modular.Redis.ParamData { paramName = commandType, paramValue = "2" }, //单工位的堆垛机，命令类型工位1搬运即可
						new GZ.Modular.Redis.ParamData { paramName = srcZs1, paramValue = task1?.FromX.ToString() ?? "" }, //排
						new GZ.Modular.Redis.ParamData { paramName = srcXs1, paramValue = task1?.FromY.ToString() ?? "" }, //列
						new GZ.Modular.Redis.ParamData { paramName = srcYs1, paramValue = task1?.FromZ.ToString() ?? ""  }, //层
						new GZ.Modular.Redis.ParamData { paramName = destZs1, paramValue = task1?.ToX.ToString() ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = destXs1, paramValue = task1?.ToY.ToString() ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = destYs1, paramValue = task1?.ToZ.ToString() ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = taskS1, paramValue = task1?.DeviceTaskNo ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = contType1, paramValue = task1?.TrayType ?? ""  },
						new GZ.Modular.Redis.ParamData { paramName = srcZs2, paramValue = task2?.FromX.ToString() ?? "" }, //排
						new GZ.Modular.Redis.ParamData { paramName = srcXs2, paramValue = task2?.FromY.ToString() ?? "" }, //列
						new GZ.Modular.Redis.ParamData { paramName = srcYs2, paramValue = task2?.FromZ.ToString() ?? "" }, //层
						new GZ.Modular.Redis.ParamData { paramName = destZs2, paramValue = task2?.ToX.ToString() ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = destXs2, paramValue = task2?.ToY.ToString() ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = destYs2, paramValue = task2?.ToZ.ToString() ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = taskS2, paramValue = task2?.DeviceTaskNo ?? "" },
						new GZ.Modular.Redis.ParamData { paramName = contType2, paramValue = task2?.TrayType ?? "" }
					}
				};

				if (Conn.YBRedis.SetQueue(group, setQueue, "", "")) //要确保位置信息、命令类型等已写入
				{
					Thread.Sleep(100);
					if (task1 != null)
					{
						Conn.YBRedis.SetValue(confirmSignal1, "1", setQueue);
					}
					if (task2 != null)
					{
						Conn.YBRedis.SetValue(confirmSignal2, "1", setQueue);
					}

					return true;
				}

				return false;
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}
		}

		/// <summary>
		/// 双工位堆垛机任务清除
		/// </summary>
		/// <param name="num">堆垛机编号</param>
		/// <param name="type">命令类型</param>
		/// <param name="wcsConfirm1">工位1确认信号</param>
		/// <param name="wcsConfirm2">工位2确认信号</param>
		/// <param name="err">错误信息</param>
		/// <returns>是否成功</returns>
		public bool ClearTask_DoubleStation(string num, string type, string wcsConfirm1, string wcsConfirm2, out string err)
		{
			err = "";
			try
			{
				string groupName = $"TC{num}.sendTcTask_{num}";
				string setQueue = $"TC{num}Queue";
				string srmNo = $"TC{num}.wcs_srmNo";
				string commandType = $"TC{num}.wcs_commandType";

				// 工位1参数
				string srcZs1 = $"TC{num}.wcs_source_Z_S1";
				string srcXs1 = $"TC{num}.wcs_source_X_S1";
				string srcYs1 = $"TC{num}.wcs_source_Y_S1";
				string destZs1 = $"TC{num}.wcs_dest_Z_S1";
				string destXs1 = $"TC{num}.wcs_dest_X_S1";
				string destYs1 = $"TC{num}.wcs_dest_Y_S1";
				string taskS1 = $"TC{num}.wcs_task_S1";
				string contType1 = $"TC{num}.wcs_goodType1";
				string confirmSignal1 = $"TC{num}.wcs_confirmSignal1";

				// 工位2参数
				string srcZs2 = $"TC{num}.wcs_source_Z_S2";
				string srcXs2 = $"TC{num}.wcs_source_X_S2";
				string srcYs2 = $"TC{num}.wcs_source_Y_S2";
				string destZs2 = $"TC{num}.wcs_dest_Z_S2";
				string destXs2 = $"TC{num}.wcs_dest_X_S2";
				string destYs2 = $"TC{num}.wcs_dest_Y_S2";
				string taskS2 = $"TC{num}.wcs_task_S2";
				string contType2 = $"TC{num}.wcs_goodType2";
				string confirmSignal2 = $"TC{num}.wcs_confirmSignal2";

				// LAST状态参数
				string setLast1 = $"TC{num}.plc_taskStatus1_LAST";
				string setLast2 = $"TC{num}.plc_taskStatus2_LAST";

				// 根据确认信号设置PLC状态
				string plcTaskStatus1 = wcsConfirm1 == "8" ? "6" : (wcsConfirm1 == "10" ? "5" : (wcsConfirm1 == "3" ? "3" : "0"));
				string plcTaskStatus2 = wcsConfirm2 == "8" ? "6" : (wcsConfirm2 == "10" ? "5" : (wcsConfirm2 == "3" ? "3" : "0"));

				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = srmNo, paramValue = int.Parse(num).ToString() },
						new GZ.Modular.Redis.ParamData { paramName = commandType, paramValue = type },
						// 工位1清零
						new GZ.Modular.Redis.ParamData { paramName = srcZs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = srcXs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = srcYs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destZs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destXs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destYs1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskS1, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = contType1, paramValue = "0" },
						// 工位2清零
						new GZ.Modular.Redis.ParamData { paramName = srcZs2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = srcXs2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = srcYs2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destZs2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destXs2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destYs2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskS2, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = contType2, paramValue = "0" }
					}
				};

				// 设置队列并更新LAST状态
				if (Conn.YBRedis.SetQueue(group, setQueue, setLast1, plcTaskStatus1) &&
				    Conn.YBRedis.SetQueue(group, setQueue, setLast2, plcTaskStatus2))
				{
					// 设置确认信号
					Conn.YBRedis.SetValue(confirmSignal1, wcsConfirm1, setQueue);
					Conn.YBRedis.SetValue(confirmSignal2, wcsConfirm2, setQueue);
					return true;
				}

				return false;
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}
		}
	}
}